package com.pt25.study.example.interfaces.facade.inner;

import com.pt25.base.dep.shardingjdbc.datasource.Datasource;
import com.pt25.base.res.ResBody;
import com.pt25.study.example.application.ShardingAppService;
import com.pt25.study.example.interfaces.assembler.ShardingAssembler;
import com.pt25.study.example.interfaces.vo.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "sharding inner api")
@RestController
@RequestMapping("/inner-api/example")
public class ShardingInnerController {

    private final static String ADD_SHARDING_TEST = "/sharding-test";
    private final static String GET_SHARDING_TEST = "/sharding-test/{id}";
    private final static String ADD_SHARDING_MS = "/sharding-ms";
    private final static String GET_SHARDING_MS = "/sharding-ms/{id}";
    private final static String ADD_SHARDING_SD = "/sharding-sd";
    private final static String GET_SHARDING_SD = "/sharding-sd/{id}";
    private final static String ADD_SHARDING_SDST = "/sharding-sdst";
    private final static String GET_SHARDING_SDST = "/sharding-sdst/{id}";
    private final static String ADD_SHARDING_SDSTMS = "/sharding-sdstms";
    private final static String GET_SHARDING_SDSTMS = "/sharding-sdstms/{id}";

    @Resource
    private ShardingAppService shardingAppService;

    @PostMapping(ADD_SHARDING_TEST)
    public ResBody addShardingTest(@RequestBody @Valid ShardingTestAddVo shardingTestAddVo) {
        shardingAppService.addShardingTest(ShardingAssembler.ShardingTestAddVo2ShardingTest(shardingTestAddVo));
        return ResBody.builder().build().ok();
    }


    @GetMapping(GET_SHARDING_TEST)
    public ResBody getShardingTest(@PathVariable("id") Long id) {
        return ResBody.builder().build().ok(shardingAppService.getShardingTest(id));
    }

    @Datasource("pt_db_ms") //指定数据源
    @PostMapping(ADD_SHARDING_MS)
    public ResBody addShardingMS(ShardingMsAddVo shardingMsAddVo) {
        shardingAppService.addShardingMs(ShardingAssembler.ShardingMsAddVo2ShardingMs(shardingMsAddVo));
        return ResBody.builder().build().ok();
    }

    @Datasource("pt_db_ms") //指定数据源
    @GetMapping(GET_SHARDING_MS)
    public ResBody getShardingMs(@PathVariable("id") Long id) {
        return ResBody.builder().build().ok(shardingAppService.getShardingMs(id));
    }


    @Datasource("pt_db_sdst") //指定数据源
    @PostMapping(ADD_SHARDING_SD)
    public ResBody addShardingSd(ShardingSdAddVo shardingSdAddVo) {
        shardingAppService.addShardingSd(ShardingAssembler.ShardingSDAddVO2ShardingSD(shardingSdAddVo));
        return ResBody.builder().build().ok();
    }

    @Datasource("pt_db_sdst") //指定数据源
    @GetMapping(GET_SHARDING_SD)
    public ResBody getShardingSd(@PathVariable("id") Long id) {
        return ResBody.builder().build().ok(shardingAppService.getShardingSd(id));
    }


    @Datasource("pt_db_sdst") //指定数据源
    @PostMapping(ADD_SHARDING_SDST)
    public ResBody addShardingSdst(ShardingSdstAddVo shardingSdstAddVo) {
        shardingAppService.addShardingSdst(ShardingAssembler.ShardingSdstAddVo2ShardingSdst(shardingSdstAddVo));
        return ResBody.builder().build().ok();
    }

    @Datasource("pt_db_sdst") //指定数据源
    @GetMapping(GET_SHARDING_SDST)
    public ResBody getShardingSdst(@PathVariable("id") Long id) {
        return ResBody.builder().build().ok(shardingAppService.getShardingSdst(id));
    }


    @Datasource("pt_db_sdstms") //指定数据源
    @PostMapping(ADD_SHARDING_SDSTMS)
    public ResBody addShardingSdstms(ShardingSdstmsAddVo shardingSdstmsAddVo) {
        shardingAppService.addShardingSdstms(ShardingAssembler.ShardingSdstmsAddVO2ShardingSdstms(shardingSdstmsAddVo));
        return ResBody.builder().build().ok();
    }

    @Datasource("pt_db_sdstms") //指定数据源
    @GetMapping(GET_SHARDING_SDSTMS)
    public ResBody getShardingSdstms(@PathVariable("id") Long id) {
        return ResBody.builder().build().ok(shardingAppService.getShardingSdstms(id));
    }

}
